package com.njym.java.security.app.rest;

import com.github.pagehelper.PageInfo;
import com.njym.java.security.app.biz.FamilyCounselorBiz;
import com.njym.java.security.app.entity.FamilyCounselor;
import com.njym.java.security.app.entity.FamilyCounselorVo;
import com.njym.java.security.common.msg.ObjectRestResponse;
import com.njym.java.security.common.rest.BaseController;
import com.njym.java.security.common.util.ResponseUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("counselor")
public class FamilyCounselorController extends BaseController<FamilyCounselorBiz, FamilyCounselor> {
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<FamilyCounselorVo> lists(@RequestParam(defaultValue = "10") int limit, @RequestParam(defaultValue = "1")int page){
        List<FamilyCounselorVo> list =  baseBiz.selectList();
        long total = PageInfo.of(list).getTotal();
        int  totalPages= (int)Math.ceil((double)total/limit);
        Map<String, Object> data = new HashMap<>(1);
        data.put("list", list);
        data.put("totalPages",totalPages);
        return new ObjectRestResponse<FamilyCounselorVo>().rel(true).data(data);

    }

    @RequestMapping(value = "/like",method = RequestMethod.GET)
    @ResponseBody
    public Object like(@RequestParam String id){
        if (!id.equals("undefined")) {
            baseBiz.like(Integer.parseInt(id));
            return new ObjectRestResponse<>().rel(true);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }
    }
}
